package com.sharainy.service.impl;


import com.sharainy.dao.AreaMapper;
import com.sharainy.pojo.Area;
import com.sharainy.service.AreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AreaServiceImpl implements AreaService {
    @Autowired
    private AreaMapper areaMapper;
    @Override
    public List<Area> selectLevel1Area(int company_id) {
        return areaMapper.selectLevel1Area(company_id);
    }

    @Override
    public List<Area> selectLevel2Area(int parent_node) {
        return areaMapper.selectLevel2Area(parent_node);
    }

    @Override
    public List<Area> selectLevel3Area(int parent_node) {
        return areaMapper.selectLevel3Area(parent_node);
    }

    @Override
    public String getLevel1NameById(String id) {
        return areaMapper.getLevel1NameById(id);
    }

    @Override
    public String getLevel2NameById(String id) {
        return areaMapper.getLevel2NameById(id);
    }

    @Override
    public String getLevel3NameById(String id) {
        return areaMapper.getLevel3NameById(id);
    }

    @Override
    public void deleteLevel1(String id) {
        areaMapper.deleteLevel1(id);
    }

    @Override
    public void deleteLevel2(String id) {
        areaMapper.deleteLevel2(id);
    }

    @Override
    public void deleteLevel3(String id) {
        areaMapper.deleteLevel3(id);
    }

    @Override
    public Area getLevel1ByName(Map<String, String> map) {
        return areaMapper.getLevel1ByName(map);
    }

    @Override
    public Area getLevel2ByName(Map<String, String> map) {
        return areaMapper.getLevel2ByName(map);
    }

    @Override
    public Area getLevel3ByName(Map<String, String> map) {
        return areaMapper.getLevel3ByName(map);
    }

    @Transactional
    @Override
    public void addAllArea(String arealevel1, String arealevel2, String arealevel3, int companyId) throws Exception {
        if (!"".equals(arealevel1)){
            long time = System.currentTimeMillis()/1000;
            Map<String, String> level1map = new HashMap<String, String>();
            level1map.put("name",arealevel1);
            level1map.put("companyId",companyId+"");
            Area area1 = areaMapper.getLevel1ByName(level1map);
            if (area1==null){
                Area level1Area = new Area();
                level1Area.setName(arealevel1);
                level1Area.setCompanyid(companyId);
                level1Area.setCreateTime(time);
                System.out.println("===================================");
                System.out.println(level1Area);
                areaMapper.addLevel1Area(level1Area);
                System.out.println(level1Area.getId());
                if (!"".equals(arealevel2)){
                    Area level2Area = new Area();
                    level2Area.setName(arealevel2);
                    level2Area.setParentnode(level1Area.getId());
                    level2Area.setCreateTime(time);
                    areaMapper.addLevel2Area(level2Area);
                    if (!"".equals(arealevel3)){
                        Area level3Area = new Area();
                        level3Area.setName(arealevel3);
                        level3Area.setParentnode(level2Area.getId());
                        level3Area.setCreateTime(time);
                        areaMapper.addLevel3Area(level3Area);
                    }
                }
            }else{
                int level1Id = area1.getId();
                if (!"".equals(arealevel2)){
                    Map<String, String> level2map = new HashMap<String, String>();
                    level2map.put("name",arealevel2);
                    level2map.put("parentNode",level1Id+"");
                    Area area2 =areaMapper.getLevel2ByName(level2map);
                    if (area2==null){
                        Area level2Area = new Area();
                        level2Area.setName(arealevel2);
                        level2Area.setParentnode(level1Id);
                        level2Area.setCreateTime(time);
                        areaMapper.addLevel2Area(level2Area);
                        if (!"".equals(arealevel3)){
                            Area level3Area = new Area();
                            level3Area.setName(arealevel3);
                            level3Area.setParentnode(level2Area.getId());
                            level3Area.setCreateTime(time);
                            areaMapper.addLevel3Area(level3Area);
                        }
                    }else{
                        int level2Id = area2.getId();
                        if (!"".equals(arealevel3)){
                            Map<String, String> level3map = new HashMap<String, String>();
                            level3map.put("name",arealevel3);
                            level3map.put("parentNode",level2Id+"");
                            Area area3 =areaMapper.getLevel3ByName(level3map);
                            if (area3==null){
                                Area level3Area = new Area();
                                level3Area.setName(arealevel3);
                                level3Area.setParentnode(level2Id);
                                level3Area.setCreateTime(time);
                                areaMapper.addLevel3Area(level3Area);
                            }else{
                                throw new Exception();
                            }
                        }else {
                            throw new Exception();
                        }
                    }
                }else{
                    throw new Exception();
                }
            }
        }
    }

    @Override
    public void updateLevel1Area(Area area) {
        areaMapper.updateLevel1Area(area);
    }

    @Override
    public void updateLevel2Area(Area area) {
        areaMapper.updateLevel2Area(area);
    }

    @Override
    public void updateLevel3Area(Area area) {
        areaMapper.updateLevel3Area(area);
    }
}
